package com.lizhi.menu.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lizhi.menu.Dto.CarDto;
import com.lizhi.menu.Dto.IdListDto;
import com.lizhi.menu.common.response.Result;
import com.lizhi.menu.domain.Car;
import com.lizhi.menu.service.ICarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 购物车Controller
 *
 * @author lizhi
 * @date 2023-05-18
 */
@RestController
@RequestMapping("/system/car")
@RequiredArgsConstructor
@Api(tags = "购物车管理")
public class CarController {
    private final ICarService carService;

    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    @ApiOperation("查询购物车列表")
    public Result<Page<Car>> list(CarDto dto) {
        return Result.ok(carService.list(dto));
    }


    /**
     * 查询购物车列表
     */
    @GetMapping("/my")
    @ApiOperation("查询我的购物车列表")
    public Result<Page<Car>> my(CarDto dto) {
        dto.setUserId(Long.parseLong(StpUtil.getLoginId().toString()));
        return Result.ok(carService.list(dto));
    }

    /**
     * 获取购物车详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取购物车详细信息")
    public Result<Car> getInfo(@PathVariable("id") Long id) {
        return Result.ok(carService.getInfo(id));
    }

    /**
     * 新增购物车
     */
    @PostMapping
    @ApiOperation("新增购物车")
    public Result<Void> add(@RequestBody Car car) {
        car.setUserId(StpUtil.getLoginId().toString());
        carService.add(car);
        return Result.ok();
    }

    /**
     * 修改购物车
     */
    @PutMapping
    @ApiOperation("修改购物车")
    public Result<Void> edit(@RequestBody Car car) {
        carService.edit(car);
        return Result.ok();
    }

    /**
     * 删除购物车
     */
    @DeleteMapping("/remove")
    @ApiOperation("删除购物车")
    public Result<Void> remove(@RequestBody IdListDto ids) {
        carService.remove(ids.getIdList());
        return Result.ok();
    }

    /**
     * 删除购物车
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除购物车")
    public Result<Void> remove(@PathVariable String id) {
        carService.remove(id);
        return Result.ok();
    }
}
